require(['./config'],()=>{
    require(['template','header','footer','bootstrap'],(template)=>{
        class PayMoney{
            constructor(){
                this.init()
                
            }
            init(){
                let cartList = localStorage.getItem('cart')
                if(cartList){
                    this.cartList=JSON.parse(cartList)
                    this.carts= this.cartList.filter(cart=>{
                        return cart.check==true && cart.num>0
                    })
                    $('#pay-shop').html(template('pay-shopTemplate',{cartList:this.carts}))
                    this.operate()
                   

                }
            }
            operate(){
                this.calTotal()
                this.toPay()
               this.calShopCart()
            }
            calTotal(){
                $('#Paymoney').html(this.carts.reduce((money,cart)=>{
                    if(cart.check) money+=cart.price*cart.num
                    return money       
                },0).toFixed(2))
            }
            toPay(){
                $('.surePay').on('click',function(){
                    if($('#inputName').val() && $('#inputPhoneNum').val() && $('#inputArea').val()){
                        location.href='/html/giveMoney.html'
                    }else{
                        alert('你需要填写完整收货信息哟')
                    }
                    
                })
            }
            calShopCart(){
                    let shopcount = 0
                    shopcount = this.carts.reduce((total, shop) => {
                        return total + shop.num
                      }, 0)
                    $('#shopNumber').html(shopcount)
                    console.log($('#shopNumber').html(),this.carts,shopcount)
            }
        }
        new PayMoney()
    })
})